using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.RecipeFramework;
using System.IO;
using EnvDTE;
using Microsoft.Practices.RecipeFramework.Extensions.CommonHelpers;
using Microsoft.Practices.RepositoryFactory.Actions.Configuration.Helpers;

namespace Microsoft.Practices.RepositoryFactory.Actions.Configuration
{
    /// <summary>
    /// Given an <see cref="T:EnvDTE.Project" /> object, obtains the <see cref="T:System.Configuration.Configuration" /> object
    /// </summary>
    public class GetWebConfigurationAction : ConfigurableAction
    {
        private Project project;
        private string template;
        private System.Configuration.Configuration configuration;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:GetWebConfigurationAction"/> class.
        /// </summary>
        public GetWebConfigurationAction()
        {
              this.template = string.Empty;
        }
 
        /// <summary>
        /// <see cref="M:Microsoft.Practices.RecipeFramework.IAction.Execute" />
        /// </summary>
        public override void Execute()
        {
            this.configuration = ConfigurationVSHelper.GetConfiguration(this.Project);
            if (this.configuration == null)
            {
                ProjectItem item = this.Project.ProjectItems.AddFromTemplate(this.Template, "Web.config");
                this.configuration = ConfigurationVSHelper.GetConfiguration(this.Project);
            }
        }

        /// <summary>
        /// <see cref="M:Microsoft.Practices.RecipeFramework.IAction.Undo" />
        /// </summary>
        public override void Undo()
        {
        }

        /// <summary>
        /// The output <see cref="T:System.Configuration.Configuration" /> object
        /// </summary>
        [Output]
        public System.Configuration.Configuration Configuration
        {
            get { return this.configuration; }
        }

        /// <summary>
        /// The project that owns the configuration file
        /// </summary>
        [Input(Required = true)]
        public Project Project
        {
          get { return this.project; }
          set { this.project = value; }
        }

        /// <summary>
        /// The template to unfold in case that the project does not have a configuration file
        /// </summary>
        [Input(Required = false)]
        public string Template
        {
            get
            {
                if (string.IsNullOrEmpty(this.template))
                {
                    this.template = Path.GetFullPath(
                        Path.Combine(
                        Environment.GetEnvironmentVariable("VS80COMNTOOLS", EnvironmentVariableTarget.Process),
                        "..\\IDE\\ItemTemplatesCache\\Web"));

                    if (DteHelperEx.IsWebCSharpProject(this.project))
                    {
                        this.template = Path.Combine(this.template, @"CSharp\1033\WebConfig.zip\WebConfig.vstemplate");
                    }
                    else
                    {
                        this.template = Path.Combine(this.template, @"VisualBasic\1033\WebConfig.zip\WebConfig.vstemplate");
                    }
                }
                return this.template;
            }
            set { this.template = value; }
        }
    } 
}
